package com.mission.schedule.my160920.widget;

import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Rect;
import android.text.Layout;
import android.text.Selection;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.EditText;

import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.cutimage.Log;
import com.mission.schedule.utils.SharedPrefUtil;

/**
 * Created by liwei on 2017/2/7.
 */

public class EditTextC extends android.support.v7.widget.AppCompatEditText {
    private int index;
    private int mSelectionStartBeforeDelete;

    private Context context;

    public EditTextC(Context context) {
        super(context);
        index = 0;
        this.context = context;
    }

    private boolean isTitle = false;

    public void setEditTitle() {
        isTitle = true;
    }

    public EditTextC(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextC(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getIndex() {
        return index;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                int x = (int) event.getX();
                int y = (int) event.getY();
                x -= getTotalPaddingLeft();
                y -= getTotalPaddingTop();
                x += getScrollX();
                y += getScrollY();

                Layout layout = getLayout();
                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);
                Selection.setSelection(getText(), off);
                break;
        }

        return super.onTouchEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_ENTER:
                if (edittextCoutomListener != null) {
                    return false;
                }
                break;
            case KeyEvent.KEYCODE_DEL:
                mSelectionStartBeforeDelete = getSelectionStart();
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onTextContextMenuItem(int id) {

        if (id == android.R.id.paste) {
            int selectionStart = getSelectionStart();
            String entertext = "";
            if (!getText().toString().isEmpty()) {
                entertext = getText().toString().substring(selectionStart, getText().toString().length());
            }

            ClipboardManager clip = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
            String text = clip.getPrimaryClip().getItemAt(0).getText().toString();
            if (!text.isEmpty()) {
                String[] items = text.split("\n");
                if (!entertext.isEmpty()) {
                    items[items.length - 1] = items[items.length - 1] + entertext;
                }
                if (items.length > 1) {
                    String fisrt = getText().toString().substring(0, selectionStart);
                    setText(fisrt + items[0]);
                    int en = index;
                    for (int i = 1; i < items.length; i++) {
                        if (!items[i].isEmpty()) {
                            if (isTitle){
                                edittextCoutomListener.enter(en, items[i]);
                                en = en + 1;
                            } else {
                                en = en + 1;
                                edittextCoutomListener.enter(en, items[i]);
                            }
                        }
                    }
                } else {
                    String fisrt = getText().toString().substring(0, selectionStart);
                    String last = getText().toString().substring(selectionStart, getText().toString().length());
                    setText(fisrt + items[0] + last);
                }
            }
            setSelection(getText().toString().length());
            return true;

        }
        return super.onTextContextMenuItem(id);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DEL:
                if (edittextCoutomListener != null) {
                    if (0 == mSelectionStartBeforeDelete) {
                        edittextCoutomListener.delete(index, getText().toString());
                    }
                }
                break;
            case KeyEvent.KEYCODE_ENTER:
                if (edittextCoutomListener != null) {
                    int selectionStart = getSelectionStart();
                    String text = getText().subSequence(selectionStart, length()).toString();
                    setText(getText().subSequence(0, selectionStart));
                    edittextCoutomListener.enter(index + 1, text);
                }
                break;
            default:
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if (edittextCoutomListener != null) {
            if (!focused && TextUtils.isEmpty(getText())) {
                edittextCoutomListener.textchange(index, false);
            } else {
                edittextCoutomListener.textchange(index, true);
            }
        }
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    EdittextCoutomListener edittextCoutomListener;

    public void setEdittextCoutomListener(EdittextCoutomListener edittextCoutomListener) {
        this.edittextCoutomListener = edittextCoutomListener;
    }

    public interface EdittextCoutomListener {
        void enter(int index, String text);

        void delete(int index, String text);

        void textchange(int index, boolean hastext);
    }
}
